import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * config.js - 系统配置 model
 * @date: 2018-10-29
 */
import { getResponse } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { queryTenantConfig as _queryTenantConfig, updateTenantConfig as _updateTenantConfig, updateOrganizationConfig as _updateOrganizationConfig, queryOrganizationConfig as _queryOrganizationConfig } from "../services/configService";
export default {
  namespace: 'config',
  state: {
    data: [],
    // tenantList: [],
    lov: {} // 存放值集

  },
  effects: {
    // 初始化查询值集
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, lovBatch, safeLovBatch, safeLov;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryMapIdpValue, {
                menuLayout: 'CPFM.MENU_LAYOUT',
                menuLayoutTheme: 'CPFM.MENU_LAYOUT_THEME',
                roleMergeFlag: 'CPFM.ENABLED_FLAG'
              });

            case 3:
              lovBatch = _context.sent;
              safeLovBatch = getResponse(lovBatch);

              if (!safeLovBatch) {
                _context.next = 10;
                break;
              }

              safeLov = {};
              Object.keys(safeLovBatch).forEach(function (lovKey) {
                safeLov[lovKey] = safeLovBatch[lovKey] || [];
              });
              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  lov: safeLov
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 当前为租户下查询系统配置
    queryOrganizationConfig: /*#__PURE__*/_regeneratorRuntime.mark(function queryOrganizationConfig(_ref2, _ref3) {
      var payload, call, put, res, data;
      return _regeneratorRuntime.wrap(function queryOrganizationConfig$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_queryOrganizationConfig, payload);

            case 4:
              res = _context2.sent;
              data = getResponse(res);
              _context2.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  data: data
                }
              });

            case 8:
              return _context2.abrupt("return", data);

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, queryOrganizationConfig);
    }),
    // 当前为租户下更新系统配置
    updateOrganizationConfig: /*#__PURE__*/_regeneratorRuntime.mark(function updateOrganizationConfig(_ref4, _ref5) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateOrganizationConfig$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_updateOrganizationConfig, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateOrganizationConfig);
    }),
    // 当前为平台查询系统配置
    queryTenantConfig: /*#__PURE__*/_regeneratorRuntime.mark(function queryTenantConfig(_ref6, _ref7) {
      var payload, call, put, res, data;
      return _regeneratorRuntime.wrap(function queryTenantConfig$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call, put = _ref7.put;
              _context4.next = 4;
              return call(_queryTenantConfig, payload);

            case 4:
              res = _context4.sent;
              data = getResponse(res);
              _context4.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  data: data
                }
              });

            case 8:
              return _context4.abrupt("return", data);

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, queryTenantConfig);
    }),
    // 当前为平台更新系统配置
    updateTenantConfig: /*#__PURE__*/_regeneratorRuntime.mark(function updateTenantConfig(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateTenantConfig$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_updateTenantConfig, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateTenantConfig);
    })
  },
  reducers: {
    // 批量条件查询值集头
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};